package com.interesting.client.support.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.interesting.client.bean.Emotions;
import com.interesting.client.bean.ResponseResult;


public class BlogEmotionsHandler extends DefaultHandler {
	
	private List<Emotions> list;
	
	private Emotions emotions;
	private ResponseResult responseresult;
	
	private String tag = null;//正在解析的元素
	
	
	public List<Emotions> getEmotionsList(){
		return list;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (tag != null) {
			String textArea = new String(ch,start,length);
			
			/**开始解析表情数据*/
			if ("phrase".equals(tag)) {
				emotions.setPhrase(textArea);
			} else if ("type".equals(tag)) {
				emotions.setType(textArea);
			} else if ("url".equals(tag)) {
				try {
					emotions.setUrl(textArea);
					String imageName = textArea.substring(textArea.lastIndexOf("/") + 1,textArea.length() - 4);
					emotions.setImageName(imageName);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else if ("is_hot".equals(tag)) {
				emotions.setIsHot(textArea);
			} else if ("is_common".equals(tag)) {
				emotions.setIsCommon(textArea);
			} else if ("order_number".equals(tag)) {
				emotions.setOrderNumber(textArea);
			} else if ("category".equals(tag)) {
				emotions.setCategory(textArea);
			} else if ("retn".equals(tag)) {
				responseresult.setRetn(textArea);
			} else if ("desc".equals(tag)) {
				responseresult.setDesc(textArea);
			}
		}
		
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		tag = null;
		if ("mb".equals(localName)) {
			
		} else if ("emotions".equals(localName)) {
			responseresult =null;
		} else if ("emotion".equals(localName)) {
			list.add(emotions);
			
			emotions = null;
		} 
	}

	@Override
	public void startDocument() throws SAXException {
		list = new ArrayList<Emotions>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if ("mb".equals(localName)) {
			responseresult = new ResponseResult();
		} else if ("emotions".equals(localName)) {
			
		} else if ("emotion".equals(localName)) {
			emotions = new Emotions();
		} 
		
		tag = localName;
	}
	
	

}
